package com.lucky.auth_boot.pojo.dto;

import com.lucky.auth_boot.pojo.po.RolesPO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO implements UserDetails {
    private Integer id;
    private Long uid;
    private String nickname;
    private String username;
    private String password;
    private Date registerTime;
    private Date lastLoginTime;
    private Integer status;
    private boolean enabled;
    private String token;
    List<RolesPO> roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for (RolesPO role : roles) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return authorities;
    }

    @Override
//    账户是否过期
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }

    @Override
//    是否锁定
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }

    @Override
//    凭证是否过期
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }

    @Override
//    用户是否失效
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }
}
